<template>
    <el-dialog
        style="pointer-events: none"
        :center="true"
        title="版本正在更新,请稍候..."
        :visible.sync="visible"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        :show-close="false"
    >
      <div>
        <el-progress
            :text-inside="true"
            :stroke-width="24"
            :percentage="percentage"
            :show-text="true"
        />
      </div>
    </el-dialog>
  </template>
   
  <script> 
  export default {
    data() {
      return {
        visible: false,
        percentage: 0
      }
    },
    mounted() {
      this.init()
    },
    methods: {
      async init() {
        let that= this;
        if(window.electronAPI){
            window.electronAPI.uploadMessage((event, args) => {
              console.log(args)
              const {payload} = args
              const {msg, status} = payload
              const handle = {
              '-1': () => {
                that.$message.error(msg)
              },
              '0': () => {
                that.$message.info(msg)
                  /*正在检测更新*/
              },
              '1': () => {
                that.$message.info(msg)
                  /* 发送下载请求 */
                  window.electronAPI.downLoadUpdate()
                  that.visible = true
              },
              '2': () => {
                that.$message.info(msg)
                  /*当前为最新版本*/
              },
              '3':() => {
                that.$message.success(msg)
              }
              }
              handle[`${status}`].call(that)
          })

          window.electronAPI.downloadProgress((event, data)=>{
              const {percent} = data
              that.percentage = Number.parseFloat((percent || 0).toFixed(2))
              if (percent >= 100) that.visible = false
          })
          setTimeout(function(){
              window.electronAPI.checkForUpdates()
          },500) 
        } 
      }
    }
  }
  </script>